package com.ruoyi.web.controller.game;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.GmArea;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.service.IGmAreaService;
import com.ruoyi.system.service.ISysMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/game")
public class GameController extends BaseController {
    @Autowired
    private IGmAreaService gmAreaService;

    @Autowired
    private ISysMemberService memberService;

    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor executor;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/area/list")
    public TableDataInfo list(GmArea gmArea)
    {
        startPage();
        List<GmArea> list = gmAreaService.selectGmAreaList(gmArea);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/member/{userName}")
    public AjaxResult memberInfo(@PathVariable(value="userName") String userName)
    {
        AjaxResult ajax = AjaxResult.success();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return AjaxResult.error("未登录");
        } else if (!loginUser.getUser().getUserName().equals(userName)) {
            return AjaxResult.error("无权限");
        }
        String sysTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date());
        ajax.put("systime", sysTime);
        SysMember sysMember = memberService.selectSysMemberByUserName(userName);
        sysMember.setPrice(null);
        ajax.put("member", sysMember);
        boolean isDaoQi = sysMember.getDaoQiTime().isBefore(LocalDateTime.now());
        executor.execute(() -> {
            SysMember member=new SysMember();
            member.setId(sysMember.getId());
            member.setUpdateTime(new Date());
            memberService.updateSysMember(member);
            //int uresout= memberService.updateSysMember(member);
            //logger.info("更新用户信息，更新结果：" + uresout);
        });
        if (isDaoQi) {
            return AjaxResult.error("会员已到期，请续费");
        } else {
            return ajax;
        }
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GmArea gmArea)
    {
        List<GmArea> list = gmAreaService.selectGmAreaList(gmArea);
        ExcelUtil<GmArea> util = new ExcelUtil<>(GmArea.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gmAreaService.selectGmAreaById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:area:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GmArea gmArea)
    {
        return toAjax(gmAreaService.insertGmArea(gmArea));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:area:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GmArea gmArea)
    {
        return toAjax(gmAreaService.updateGmArea(gmArea));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:area:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gmAreaService.deleteGmAreaByIds(ids));
    }
}
